export default class PreloadScene extends Phaser.Scene {
    preload() {
        // 加载背景
        for (let i = 1; i <= 3; i++) {
            this.load.image(`bg${i}`, `assets/images/backgrounds/bg${i}.png`);
        }

        // 加载猫咪动画帧
        [1, 2, 3].forEach(catId => {
            const frameCount = catId === 2 ? 8 : 6; // cat_02有8帧
            for (let i = 1; i <= frameCount; i++) {
                this.load.image(
                    `cat_${catId}_${i.toString().padStart(2, '0')}`, 
                    `assets/images/cats/cat_${catId}_${i.toString().padStart(2, '0')}.png`
                );
            }
        });

        // 加载UI元素
        this.load.image('button', 'assets/images/ui/buttons/button.png');
        this.load.image('button2', 'assets/images/ui/buttons/button2.png');
        this.load.image('coffee', 'assets/images/ui/items/coffee1.png');
        this.load.image('coin', 'assets/images/ui/items/coin.png');
        this.load.image('order', 'assets/images/ui/order.png');

        // 加载粒子效果
        this.load.image('particle', 'assets/images/particles/sparkle.png');
    }

    create() {
        // 注册猫咪动画
        [1, 2, 3].forEach(catId => {
            this.anims.create({
                key: `cat_${catId}_anim`,
                frames: this.generateCatFrames(catId),
                frameRate: 10,
                repeat: -1
            });
        });

        this.scene.start('MainScene');
    }

    generateCatFrames(catId) {
        const frameCount = catId === 2 ? 8 : 6;
        return Array.from({length: frameCount}, (_, i) => ({
            key: `cat_${catId}_${(i+1).toString().padStart(2, '0')}`,
            frame: 0
        }));
    }

    preload() {
        // 获取进度条DOM元素
        const progressBar = document.getElementById('progress');
        
        // 更新加载进度
        this.load.on('progress', (value) => {
            progressBar.style.width = `${value * 100}%`;
        });
        
        // 加载完成后隐藏进度条
        this.load.on('complete', () => {
            document.getElementById('loading-bar').style.display = 'none';
        });
    }
}